import java.io.*;
import java.util.*;

import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class ContactList extends JPanel
{
	File temp = new File ("contacts");
	String path = temp.getAbsolutePath();

	private Contact[] contactList;
	private int storage = 999, saved=0;

	private JPanel tagP, nameTagP, mobileTagP, emailTagP;
	private JLabel nameTagL, mobileTagL, emailTagL;

	private JPanel saveinfoP;
	private JLabel saveinfoL;

	public int selected = -1;
	

	public ContactList ()
	{
		setBackground (Color.white);
		setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));

		contactList = new Contact[storage];


		tagP = new JPanel ();
			tagP.setBackground (Color.white);

		nameTagP = new JPanel ();
			nameTagP.setBackground (new Color (255,238,238));
		nameTagL = new JLabel ("                       NAME                       ");
		nameTagP.add (nameTagL);

		mobileTagP = new JPanel ();
			mobileTagP.setBackground (new Color (255,238,238));
		mobileTagL = new JLabel ("                     MOBILE                     ");
		mobileTagP.add (mobileTagL);

		emailTagP = new JPanel ();
			emailTagP.setBackground (new Color (255,238,238));
		emailTagL = new JLabel ("                     E - MAIL                     ");
		emailTagP.add (emailTagL);


		tagP.add (Box.createRigidArea (new Dimension (21,0)));
		tagP.add (nameTagP);
		tagP.add (mobileTagP);
		tagP.add (emailTagP);

		add (tagP);
		add (Box.createRigidArea (new Dimension (0,3)));

	
		ButtonGroup c_group = new ButtonGroup ();
		EventsListener listener = new EventsListener();

		for (int i=0; i<storage; i++)
		{
			contactList[i] = new Contact (" ", " ", " ", " ", " ", " ",  " ", " ", " ", " ", " ");
				c_group.add (contactList[i].contactRB);
				contactList[i].contactRB.addActionListener (listener);
				contactList[i].contactRB.setVisible (false);
			
			contactList[i].order = i;
			add (contactList[i]);
			add (Box.createRigidArea (new Dimension (0,3)));
		}

		callRecentInfo();


		saveinfoP = new JPanel ();
			saveinfoP.setBackground (Color.white);
		saveinfoL = new JLabel ("Totally " + saved + " contact(s) is(are) already created, and "
												+ (storage-saved) + " contact(s) can be created more.");
		saveinfoP.add (Box.createRigidArea (new Dimension (180,0)));
		saveinfoP.add (Box.createHorizontalGlue());
		saveinfoP.add (saveinfoL);

		add (Box.createRigidArea (new Dimension (0,7)));
		add (Box.createVerticalGlue());
		add (saveinfoP);
	}




	private class EventsListener implements ActionListener
	{
		public void actionPerformed (ActionEvent event)
		{
			Object selected_source = event.getSource ();

			for (int i=0; i<saved; i++)
			{
				if (selected_source == contactList[i].contactRB)
					selected = contactList[i].order;
			}
		}
	}

	private void clearList ()
	{	
		for (int i=0; i<saved; i++)
		{
			contactList[i].changeContact (" ", " ", " ", " ", " ", " ",  " ", " ", " ", " ", " ");
				contactList[i].contactRB.setVisible (false);
		}

		saved = 0;
	}



	public void callRecentInfo ()
	{
		clearList ();

		File[] files = temp.listFiles();
		for (int i=0; i<files.length; i++)
		{
			try
			{
				String filename = files[i].getName();
				Scanner infoscan = new Scanner (new File (path, filename));
						infoscan.useDelimiter ("&&&&");

				String f_name = infoscan.next();
				String l_name = infoscan.next();
				String company = infoscan.next();
				String department = infoscan.next();
				String position = infoscan.next();
				String mobile = infoscan.next();
				String phone = infoscan.next();
				String fax = infoscan.next();
				String email = infoscan.next();
				String address = infoscan.next();
				String homepage = infoscan.next();

				contactList[i].changeContact (f_name, l_name, company, department, position, mobile, phone, fax, email, address, homepage);
				contactList[i].contactRB.setVisible (true);
			
				saved++;
			}
			catch (IOException ioe)	{	JOptionPane.showMessageDialog (null, "System failed to call contacts info.");	}
		}
	}



	public void createContact (String f_name, String l_name, String company, String department, String position, String mobile, String phone, String fax, String email, String address, String homepage)
	{
		if (saved < storage)
		{
			contactList[saved].changeContact (f_name, l_name, company, department, position, mobile, phone, fax, email, address, homepage);
				contactList[saved].contactRB.setVisible (true);
			
			saved++;
			clearList ();
			callRecentInfo ();
			saveinfoL.setText ("Totally " + saved + " contact(s) is(are) already created, and " + (storage-saved) + " contact(s) can be created more.");
		}

		else
			JOptionPane.showMessageDialog (null, "Contacts are full, so that you cannot create a new contact any more.");
	}


	public void deleteContact (int x)
	{
		for (int i=x; i<saved; i++)
			contactList[i].changeContact (contactList[i+1].getFName(), contactList[i+1].getLName(),
															contactList[i+1].getCompany(), contactList[i+1].getDepartment(), contactList[i+1].getPosition(),
															contactList[i+1].getMobile(), contactList[i+1].getPhone(), contactList[i+1].getFax(),
															contactList[i+1].getEmail(), contactList[i+1].getAddress(), contactList[i+1].getHomepage());
		
		contactList[saved-1].changeContact (" ", " ", " ", " ", " ", " ",  " ", " ", " ", " ", " ");
		contactList[saved-1].contactRB.setVisible (false);

		saved--;
		saveinfoL.setText ("Totally " + saved + " contact(s) is(are) already created, and "
										+ (storage-saved) + " contact(s) can be created more.");
	}



	public String getFName (int x)			{ return (contactList[x].getFName()); }
	public String getLName (int x)			{ return (contactList[x].getLName()); }
	public String getCompany (int x)		{ return (contactList[x].getCompany()); }
	public String getDepartment (int x)	{ return (contactList[x].getDepartment()); }
	public String getPosition (int x)		{ return (contactList[x].getPosition()); }
	public String getMobile (int x)			{ return (contactList[x].getMobile());	}
	public String getPhone (int x)			{ return (contactList[x].getPhone());	}
	public String getFax (int x)				{ return (contactList[x].getFax()); }
	public String getEmail (int x)			{ return (contactList[x].getEmail()); }
	public String getAddress (int x)		{ return (contactList[x].getAddress()); }
	public String getHomepage (int x)	{ return (contactList[x].getHomepage()); }

	public int getSaved()	{	return saved;	}
}